﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Fin;
using ESOL.WinUI.Sales;

namespace ESOL.WinUI.Finance
{
    public partial class frmManageReturnAccountOrder : BaseForm.frmBaseMdiBusinessForm
    {
        #region Vars
        private FinReturnAccountOrderEntity mSelectedReturnAccount;
        #endregion

        #region Cons
        public frmManageReturnAccountOrder()
        {
            InitializeComponent();
        }
        #endregion

        #region Func
        private void bindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "FRAO", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colStatus, "FRAO", true);
        }
        private string GetCondition()
        {
            string condition = "";

            if (!string.IsNullOrEmpty(txtReturnAccount.Text.Trim()))
            {
                condition += " AND A.ORDER_CODE LIKE '%" + txtReturnAccount.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtReturnOrderCode.Text.Trim()))
            {
                condition += " AND B.ORDER_CODE LIKE '%" + txtReturnOrderCode.Text + "%'";
            }
            if (dtpStart.EditValue != null)
            {
                condition += " AND A.ORDER_DATE>='" + dtpStart.EditValue + "'";
            }
            if (dtpStart.EditValue != null)
            {
                condition += " AND A.ORDER_DATE<='" + dtpStart.EditValue + "'";
            }
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND A.CUSTOMER_ID = '" + customer.CustomerId + "' ";
            }
            if (txtApplicationCode.Tag != null && txtApplicationCode.Tag.GetType() == typeof(SalesProdPlanApplicationEntity))
            {
                SalesProdPlanApplicationEntity prodEntity = (SalesProdPlanApplicationEntity)txtApplicationCode.Tag;
                condition += " AND B.APPLICATION_ID = '" + prodEntity.ApplicationId + "' ";
            }
            if (txtReturnOrderCode.Tag != null && txtReturnOrderCode.GetType() == typeof(SalesReturnItemEntity))
            {
                SalesReturnOrderEntity ReturnEntity = (SalesReturnOrderEntity)txtReturnOrderCode.Tag;
                condition += " AND A.SALES_RETURN_ORDER_ID ='" + ReturnEntity.Id + "'";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND A.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "ORDER_CODE DESC";

            DataTable dt = FinReturnAccountOrderBs.GetByCondtion(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }

        private void ResetButtonEnble()
        {
            btnNew.Enabled = false;
            btnModify.Enabled = false;
            btnApprove.Enabled = false;
            btnDelete.Enabled = false;
            if (mSelectedReturnAccount.Status == Common.ReturnAccountOrder.FRAO01.ToString() || mSelectedReturnAccount.Status == Common.ReturnAccountOrder.FRAO02.ToString())//草稿 审核中
            {
                btnModify.Enabled = true;
                btnApprove.Enabled = true;
            }
            if (mSelectedReturnAccount.Status == Common.ReturnAccountOrder.FRAO04.ToString())
            {
                btnDelete.Enabled = true;
            }
        }

        #endregion

        private void frmManageReturnAccountOrder_Load(object sender, EventArgs e)
        {
            bindCombobox();
            BindDataGrid();

        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedReturnAccount = new FinReturnAccountOrderEntity(dr);
            ResetButtonEnble();

        }
        protected override void Click_Query()
        {
            BindDataGrid();
        }
        protected override void Click_New()
        {
            frmMaintainReturnAccountOrder frm = new frmMaintainReturnAccountOrder();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Modify()
        {
            if (mSelectedReturnAccount == null)
            {
                MessageBox.Show("请选择一个退货账款单", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainReturnAccountOrder frm = new frmMaintainReturnAccountOrder();
            frm.SelectedFinReturnAccountOrder = mSelectedReturnAccount;
            frm.ThisActionType = ActionType.Modify;
            DialogResult dr=frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Delete()
        {
            if (mSelectedReturnAccount == null)
            {
                MessageBox.Show("请选择一个退货账款单", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
              DialogResult dr = MessageBox.Show("确认删除？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedReturnAccount.Status = Common.ReturnAccountOrder.FRAO05.ToString();
                mSelectedReturnAccount.Modified = DateTime.Now;
                mSelectedReturnAccount.ModifiedBy = CommonParameter.CurrentUser.PersonId;

                bool successful = FinReturnAccountOrderBs.UpdateFinReturnAccountOrder(mSelectedReturnAccount);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        protected override void Click_Approve()
        {
            if (mSelectedReturnAccount == null)
            {
                MessageBox.Show("请选择一个退货账款单", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainReturnAccountOrder frm = new frmMaintainReturnAccountOrder();
            frm.SelectedFinReturnAccountOrder = mSelectedReturnAccount;
            frm.ThisActionType = ActionType.Approve;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                txtCustomer.Tag = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                txtCustomer.Tag = null;
            }
        }

        private void txtApplicationCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductPlanApplicationQuery frm = new frmMaintainProductPlanApplicationQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtApplicationCode.Text = frm.SelectedProdPlanApplication.ApplicationCode;
                txtApplicationCode.Tag = frm.SelectedProdPlanApplication;
            }
            else
            {
                txtApplicationCode.Text = "";
                txtApplicationCode.Tag = null;
            }
        }

    }
}